/**
 * Silencing the alert function
 */
window.alert = function() {};

/**
 * jQuerify the game
 */

// Backup native $ function
/*_$ = $;

getScript('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {
	$.noConflict();
	
	// Restore native $ function
	$ = _$;
});*/

function getScript(url, callback) {
	var script = document.createElement('script'),
		head = document.getElementsByTagName('head')[0];

	script.type = 'text/javascript';
	script.src = url;
	script.onload = callback;

	head.insertBefore(script, head.childNodes[0]);
}

/**
 * Capture timer
 */
function actiontimer(tvalue, updt) {
	var value = tvalue / 1000,
		battleBtn = document.getElementById('battlebutton'),
		autoBtn = document.getElementById('autobattlebutton'),
		timer = document.getElementById('actiontimer');
	
	value -= .5;
	
	if (!value) {
		actioninprogress = 0;
		timer.innerHTML = value.toFixed(1);
		
		if (battleBtn) {
			battleBtn.disabled = false;
			
			autoBtn && (autoBtn.disabled = false);
			(chatfocused == 0) && (battleBtn.focus());
		}
		
		(autoattack == 1) && sendData('battle.php?id='+autoattackid+'&autoattack=2');
		(autoattack == 2) && sendData('battle.php?id='+autoattackid+'&autoattack=3');
	}
	else {
		timer.innerHTML = '<span style="color: #FFFF00; font-weight: bold;">' + value.toFixed(1) + '</span>';
		
		battleBtn && (battleBtn.disabled = true);
		autoBtn && (autoBtn.disabled = true);
		
		atimer = setTimeout("actiontimer(" + (value * 1000) + ")", 500);
	}
}

/**
 * Capturing chat messages
 */
function botHandleResponse4() {
	if (chat.readyState == 4) {
		var response = chat.responseText,
			msgs = response.split('|');
		
		for (var i = 0; i < msgs.length; i++) {
			if (!msgs[i]) {
				continue;
			};
			
			// Event found
			if (msgs[i].indexOf('<b>Event</b>') >= 0) {
				msgs[i] = msgs[i].replace(/\((\d+)\,(\d+)\)/, function(match, x, y) {
					return '<a href="javascript:walkTo(' + x + ', ' + y + ')">' + match + '</a>';
				});
			}
			
			// <span style="color: #33FFFF"><b>Event</b>: A fearsome LostShadowMaster has been spotted at (289,393) and a reward of 4,000 platinum will be awarded to the player that lands the final blow. A further 40,000 platinum will be distributed to everyone who helps to take it down.</span>
		};
		
		chat.responseText = msgs.join('|');
	}
	
	// Calling original handler
	handleResponse4();
}

chat.onreadystatechange = botHandleResponse4;

/**
 * Main response
 */
function botHandleResponse1() {
	if ((http.readyState == 4 && whichHandle == 1) || (recap.readyState == 4 && whichHandle == 2)) {
		var response = whichHandle == 1 ? http.responseText : recap.responseText,
			responseParts = response.split('|');
		
		while (responseParts.length) {
			var type = responseParts.shift(),
				content = responseParts.shift();
		}
	}
	
	handleResponse1();
}

http.onreadystatechange = recap.onreadystatechange = botHandleResponse1;

/**
 * Trying to fix the problem with headers
 */
function sendChat2(url, params) {
	chat2.open("POST",url,true);
    chat2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    chat2.setRequestHeader("X-Requested-With", "tlrreq");
    chat2.send(params);
    chat2.onreadystatechange=handleResponse5;
}

function sendMisc(url, params) {
    misc.open("POST",url,true);
    misc.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    misc.setRequestHeader("X-Requested-With", "tlrreq");
    misc.send(params);
    misc.onreadystatechange=handleResponse6;
}

function sendRecap(url, params) {
    whichHandle = 2;
    recap.open("POST",url,true);
    recap.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    recap.setRequestHeader("X-Requested-With", "tlrreq");
    recap.send(params);
    recap.onreadystatechange=handleResponse1;
}
